package com.bolun998300.personal_blog_springboot.service;

import com.bolun998300.personal_blog_springboot.dao.CommentDao;
import com.bolun998300.personal_blog_springboot.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentDao commentDao;

    public Page<Comment> listByPage(int page,int size,String operate){
        Sort sort = new Sort(Sort.Direction.DESC,"comment_date");
        Pageable pageable = new PageRequest(page-1,size);
        Page<Comment> pageFromJpa = null;
        if("all".equals(operate)){
            pageFromJpa = commentDao.findAllByOrderByCommentDateDesc(pageable);
        }else if("notReview".equals(operate)){
            pageFromJpa = commentDao.findByStateOrderByCommentDateDesc(0,pageable);
        }
        return pageFromJpa;
    }


    public Comment get(int id){
        return commentDao.findOne(id);
    }

    public void update(Comment comment){
        commentDao.save(comment);
    }

    public void save(Comment comment){
        commentDao.save(comment);
    }

    public List<Comment> getCommentsByBlogId(int blogId){
        return commentDao.findByBlogIdAndStateOrderByCommentDateDesc(blogId,1);
    }




}
